const tool = require('./tool.js');
var params = {};
//一轮评论多少个视频
maxplNum = 30
//随机停留的时间
randomTime = 15
//重复进入次数
var enterVedioCount = 0;
//评论失败的次数
var failCount = 0;
function main (p) {
    params = p;
    init()
}

//启动小红书
function init () {
    tool.restartApp("小红书")
    sleep(5000)
    if (textContains("打开通知").exists()) {
        tool.log("取消通知")
        back()
    }
    if (textContains("升级").exists()) {
        tool.log("取消升级")
        back()
    }
    search()
}

//搜索
function search () {
    tool.log("开始搜索")
    if (tool.clickDesc("搜索")) {
        tool.log("点击搜索成功")
        sleep(2000)
        tool.log("搜索词：" + params.searchWord)
        setText(random(1, 10) + params.searchWord + random(1, 10))
        sleep(500)
        tool.clickText("搜索")
        // press(254, random(400, 1700), 10) //随机点一个
        sleep(4000)
        enterScreen()
    } else {
        tool.log("搜索失败")
        init()
    }
}

//筛选条件
function enterScreen () {
    tool.log("开始筛选")
    if (press(1152, 336, 100)) {
        sleep(500)
        if (tool.clickText("最新")) {
        }
        if (tool.clickText("图文")) {
        }
        if (tool.clickText("完成",1000)) {
        }
        sleep(3000)
        press(300, 2000, 10)
        sleep(1000)
        pinglun()
    } else {
        tool.log("未找到筛选按钮")
        search()
    }
}




//开始评论
function pinglun () {
    try {
        tool.log("开始评论")
        let nickName = ""
        for (let i = 0; i < maxplNum; i++) {
            press(random(50, 1100), random(500, 2300), 100)
            sleep(2000)
            if (!id("nickNameTV").exists()) {
                back()
                sleep(2000)
                tool.log("进入图文失败，退出")
                failCount++
                if (failCount >= 3) {
                    failCount = 0
                    init()
                    return
                }
                continue
            }
            let name = id("nickNameTV").findOne().text()
            if (name && nickName != name) {
                nickName = name
                enterVedioCount = 0
                if (sendPinglun(params.pinglun)) {
                    tool.log("评论成功")
                    failCount = 0
                    sleep(1000, randomTime * 1000)
                } else {
                    tool.log("评论失败")
                    failCount++
                    if (failCount >= 3) {
                        failCount = 0 
                        init()
                        return
                    }
                }
            } else {
                tool.log("评论失败，重复评论")
                enterVedioCount++
                if (enterVedioCount >= 3) {
                    tool.log("重复进入超过3次，退出")
                    enterVedioCount = 0
                    init()
                    return
                }
            }
            back()
            sleep(500)
            nextLive(5000)
        }
        init()
    } catch (e) {
        tool.log("评论异常：" + e)
        init()
    }

}


//发送评论
function sendPinglun (text) {
    sleep(500)
    if (tool.clickText("说点什么..."), 2000) {
        tool.log("打开评论")
        sleep(500)
        text = addRandomDigit(text)
        setText(text)    //输入评论
        sleep(1000)
        if (click("发送")) {      //发送评论
            sleep(1000)
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}


function addRandomDigit(str) {
    let result = '';
    for (let i = 0; i < str.length; i++) {
      result += str[i];
      result += Math.floor(Math.random() * 10);
    }
    return result;
  }


//刷新
function nextLive (time) {
    sleep(1000);
    let startX = random(200, 300)
    let startY = random(600, 700)
    let endX = random(300, 350)
    let endY = random(1500, 1600)
    swipe(startX, startY, endX, endY, 200)
    sleep(time)
    tool.log("刷新")
}


exports.main = main;